<template>
  <div>
    <my-breadcrumb :items="['用户管理', '用户等级管理']"/>
    <search :formItems="searchItems" ref="search" @search="getList"/>
    <el-table :data="levels" @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="等级"
        prop="level">
      </el-table-column>
      <el-table-column
        label="用户名称"
        prop="name">
      </el-table-column>
      <el-table-column
        label="用户类型"
        prop="isbusiness">
        <template slot-scope="scope">
          {{ scope.row.isbusiness === '1' ? '商家' : '普通用户' }}
        </template>
      </el-table-column>
      <el-table-column
        label="用户积分"
        sortable="custom"
        prop="nowexperience">
      </el-table-column>
      <el-table-column
        label="操作"
        prop="id">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row.id)">修改积分</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'level',
  data () {
    return {
      searchItems: [
        {
          label: '用户名称',
          key: 'name',
          type: 'input'
        },
        {
          label: '手机号码',
          type: 'input',
          key: 'phone'
        }
      ], // 检索结构
      levels: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      lifting: 'nowexperience desc'
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, type: '1', orderby: this.lifting}
      this.axios.get('/type/getmemberarr', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.levels = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toUpdate (id) {
      this.$prompt('请输入积分值', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.axios.get('/Member/editlevel', {params: {id, nowexperience: value}})
          .then(res => {
            if (res.data.status === '1' || res.data.status === 1) {
              this.getList()
              this.$message.success('修改成功')
            }
          })
      })
    },
    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
